<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>所有子字符串美丽值之和</title>
</head>
<body>
  <script>
    // 暴力方法：用双循环
    // charCodeAt：返回指定位置的字符的 Unicode 编码
    // Number.MAX_SAFE_INTEGER：最大的安全整数（253 – 1）
    function fn(s) {
      let b = s.charCodeAt(0), ans = 0, n = s.length
      for (let i = 0; i < n; i++) {
        let m = new Array(26).fill(0);
        for (let j = i; j < n; j++) {
          m[s.charCodeAt(j) - b]++;
          let max = 0, min = Number.MAX_SAFE_INTEGER;
          for (let z of m) {
            if (z) {
              max = Math.max(max, z);
              min = Math.min(min, z);
            }
          }
          ans += max - min;
        }
      }
      return ans;
    }
    console.log(fn('aabcb'))
  </script>
</body>
</html>